<?php

    /**
    * @denp: 
    * 1依赖数据库结构
    * 2.依赖orm类
    * 3.依赖ajax函数
    * 4.依赖一个controler _geo
    * 
    * 
    * @var mixed
    */
    $app = TIRI_APP::getInstance();
    $app->loadService('orm');

    class orm_province extends orm{

        public function __construct(){
            $this->table = 'geo_province';
            $this->primary_key = 'pid';
            $this->fields = array('pid'=>'','provinceID'=>'','pname'=>'');

        }
    }
    class orm_city extends orm{
        public function __construct(){
            $this->table = 'geo_city';
            $this->primary_key = 'cid';

            $this->fields=array('cid' =>'','cityID'=>'','city'=>'','provinceID'=>'');
        }
    }
    class orm_area extends orm{
        public function __construct(){
            $this->table = 'geo_area';
            $this->primary_key = 'aid';

            $this->fields= array('aid'=>'','areaID'=>'','area'=>'','cityID'=>'');
        }
    }





    //获取省一级的地区select下拉框
    //同时输出js，驱动同步更新同页面的城市列表，修改 #city_id的元素
    function return_select_provience_id($selected = -1, $name = '',$class =''){
        $orm_p = new orm_province();
        $all_p = $orm_p ->fetch_all();
        $ret = '

        <script>
        function reload_city(pid){
        var a = new ajax();
        a.url = "'.U('_geo','get_city_select_list').'";
        a.data = {"pid":pid,"class":"'.$class.'"};
        a.cb = function(result){$("city_div").innerHTML = result;};
        a.get();
        }

        </script>
        <select name="'.$name.'" onchange="reload_city(this.value)" class="'.$class.'">';
        foreach($all_p as $p){

            $ret .='<option value="'. $p->provinceID.'"';
            if($p->provinceID == $selected){
                $ret .= 'selected ="selected"';
            }
            $ret .= '>'.$p->pname.'</option>'."\n";
        }
        $ret .='</select>';
        return $ret;
    }

    //获取省一级的地区select下拉框
    function return_select_city_id($province_id, $selected = -1,$name ='',$class =''){
        if(empty($province_id))
            return null;
        $orm_c = new orm_city();
        $orm_c ->provinceID = $province_id;
        $all_c = $orm_c -> get_all_match();
        if(!$all_c)
            return null;
        $ret ='
        <select name="'.$name.'" onchange="reload_area(this.value);" class="'.$class.'">
        <option>请选择</option>';
        foreach($all_c as $c){

            $ret .='<option value="'. $c->cityID.'"';
            if($c->cityID == $selected){
                $ret .= 'selected ="selected"';
            }
            $ret .= '>'.$c->city.'</option>'."\n";
        }
        $ret .='</select>';
        return $ret;
    }

    //获取省一级的地区select下拉框
    function return_select_area_id($city_id, $selected = -1){
        if(empty($city_id))
            return null;
        $orm_a = new orm_area();
        $orm_a ->cityID = $city_id;
        $all_a = $orm_a -> get_all_match();

        $ret = '
        <select name="area_id"><option>请选择</option>';
        foreach($all_a as $a){

            $ret .='<option value="'. $a->areaID.'"';
            if($a->areaID == $selected){
                $ret .= 'selected ="selected"';
            }
            $ret .= '>'.$a->area.'</option>'."\n";
        }
        $ret .='</select>';
        return $ret;
    }
?>
